<template>
	<view class="uni-common-pb uni-padding-wrap uni-common-mt">
		<view class="content">
			<text class="title0">{{detail.subtitle}}</text>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">工单编号</view>
			<view class="title">{{detail.workordernumber}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">产品品号</view>
			<view class="title">{{detail.productname}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">全长</view>
			<view class="title">{{detail.fulllength}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">规格名</view>
			<view class="title">{{detail.specname}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">发料记录</view>
			<view class="title">{{detail.issuerecord}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">备注</view>
			<view class="title">{{detail.info}}</view>
		</view>

		<view class="line-item"></view>

		<view class="uni-form-item uni-row">
			<view class="title">制程开始时间</view>
			<view class="title">{{detail.starttime}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">制程完成时间</view>
			<view class="title">{{detail.endtime}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">操作者</view>
			<view class="title">{{detail.processlog.step_operator}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">工时</view>
			<view class="title">{{detail.step_time}}&nbsp;&nbsp;小时</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">序号</view>
			<view class="title">{{detail.processlog.step_number}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">制程代号</view>
			<view class="title">{{detail.processlog.step_code}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">制程名称</view>
			<view class="title">{{detail.processlog.step_name}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">良品</view>
			<view class="title">{{detail.processlog.step_num_ok}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">不良</view>
			<view class="title">{{detail.processlog.step_num_ng}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">原料不良</view>
			<view class="title">{{detail.processlog.step_num_pm}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">报废断针</view>
			<view class="title">{{detail.processlog.step_num_scrap_a}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">报废其它</view>
			<view class="title">{{detail.processlog.step_num_scrap_b}}</view>
		</view>

		<view class="uni-form-item uni-row">
			<view class="title">报废原因</view>
			<view class="title2">{{detail.processlog.scrap_content}}</view>
		</view>
		<view class="uni-form-item uni-row">
			<view class="title">备注</view>
			<view class="title2">{{detail.processlog.content}}</view>
		</view>

		<view class="line-item"></view>
		<form @submit="formSubmit">

			<view class="uni-form-item uni-row">
				<view class="title">报废数量（品保）</view>
				<input class="uni-input" name="pb_num_ng" v-model="pb_num_ng" type="number" placeholder="请输入报废品数" />
			</view>
			<view class="uni-form-item uni-row">
				<view class="title">备注（品保）</view>
				<textarea class="uni-textarea" name="pb_content" v-model="pb_content" placeholder="请输入备注"></textarea>
			</view>

			<view class="uni-flex uni-row">
				<button type="primary" formType="submit" style="flex: 1 1 0%;">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detail: [],
				pb_name: "",
				pb_num_ng: 0,
				pb_content: "",
			};
		},
		onLoad(e) {
			// 目前在某些平台参数会被主动 decode，暂时这样处理。
			console.log(e.detail);
			try {
				this.detail = JSON.parse(decodeURIComponent(event.detail));
			} catch (error) {
				this.detail = JSON.parse(e.detail);
			}

			this.detail.starttime = this.formatUnixtimestamp(this.detail.processlog.starttime);
			this.detail.endtime = this.formatUnixtimestamp(this.detail.processlog.endtime);
			this.detail.step_time = '--';
			if (this.detail.processlog.endtime > 0) {
				this.detail.step_time = Math.round((this.detail.processlog.endtime - this.detail.processlog.starttime) / 3600);
			}

			this.pb_name = this.detail.processlog.pb_name;
			this.pb_num_ng = this.detail.processlog.pb_num_ng;
			this.pb_content = this.detail.processlog.pb_content;
		},
		methods: {
			formatUnixtimestamp: function(e) {
				if (e == 0) {
					return '--';
				}

				var unixtimestamp = new Date(e * 1000);
				var year = 1900 + unixtimestamp.getYear();
				var month = "0" + (unixtimestamp.getMonth() + 1);
				var date = "0" + unixtimestamp.getDate();
				var hour = "0" + unixtimestamp.getHours();
				var minute = "0" + unixtimestamp.getMinutes();
				var second = "0" + unixtimestamp.getSeconds();
				return year + "-" + month.substring(month.length - 2, month.length) + "-" + date.substring(date.length - 2, date.length) +
					" " + hour.substring(hour.length - 2, hour.length) + ":" +
					minute.substring(minute.length - 2, minute.length) + ":" +
					second.substring(second.length - 2, second.length);
			},
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				this.updateprocesslog();
			},
			updateprocesslog: function() {
				const requestTask3 = uni.request({
					url: this.neiUrl + '/api/workorder/updateprocesslog_pb',
					data: {
						"workorder_id": this.detail.workorder_id,
						"step_code": this.detail.processlog.step_code,
						"pb_name": this.pb_name,
						"pb_num_ng": this.pb_num_ng,
						"pb_content": this.pb_content
					},
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(JSON.stringify(res.data));
						uni.showToast({
							title: '制程状态已经提交！',
							icon: 'success',
							mask: true,
							duration: 2000
						});
						console.log("22");
						var pages = getCurrentPages();
						var currPage = pages[pages.length - 1]; //当前页面
						var prevPage = pages[pages.length - 2]; //上一个页面
						//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
						prevPage.setData({
							refresh: "1"
						});
						uni.navigateBack();
					}
				});
			}
		}
	}
</script>

<style>
	.line-item {
		height: 1upx;
		background: #999999;
		color: #FFF;
	}

	.title {
		font-size: 30upx;
		width: 265upx;
	}

	.title2 {
		font-size: 30upx;
		width: 265upx;
		height: 200upx;
	}

	.text-box {
		min-height: 200upx;
		width: 100%;
		background-color: #FFFFFF;
		justify-content: left;
		align-items: left;
		text-align: left;
		font-size: 30upx;
		color: #353535;
		line-height: 1.8;
	}
</style>
